package nodes;


import compiler.Compiler;
import compiler.SymbolTable;
import compiler.Yytoken;

import descriptors.AbstractDescr;
import descriptors.VarDescr;

public class IdentNode extends ExpressionNode {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private String ident;
	
	@Override
	public AbstractDescr compile(
			SymbolTable symbolTable, Compiler compiler) {
		AbstractDescr descr=symbolTable.search(compiler.level(), ident);
		if(descr instanceof VarDescr){
			compiler.write("PUSHI, "+((VarDescr)descr).address());
			if (descr.getLevel() == compiler.level()) {
				compiler.write("GETFP");
				compiler.write("ADD");
			} else {
				compiler.write("PUSHI, " + descr.getLevel());
				compiler.write("GETSL");
				compiler.write("ADD");
			}
		}
		return descr;
	}
	
	public IdentNode(Yytoken token){
		super(token.line(),token.column());
		this.ident=token.value();
	}
	
	public String ident(){
		return ident;
	}

	@Override
	public void print() {
		trace("IdentNode: "+ident);
		unindent();
		
	}

}
